var Application = new Class({
	api_key : API_KEY,
	secret_key : SECRET_KEY,
	api: null,
	friendList: null,
    initialize: function() {
		// bind events
		$('proceed').disabled = true;
		$('showFriends').disabled = true;
		$('findFriends').disabled = true;
		$('errors').onmouseover = function(event){
		    var event = new Event(event);
		    new Fx.Style(document.body, 'height').start(50,200);
		}.bind(this);
		$('errors').onmouseout = function(event){
		    var event = new Event(event);
		    new Fx.Style(document.body, 'height').start(200,50);
		}.bind(this);
		$('showFriends').onclick = function(event) {
			this.api.getFriends(function(friends) {
					this.api.getInfo(friends.join(","),
								     "first_name,last_name", 
								     function(people) { 
								     	this.friendList = people; 
										$('findFriends').disabled = false;
									}.bind(this));
			}.bind(this));
		}.bind(this);
		
		$('proceed').onclick = function(event) {
			this.api.getSession(this.readySession.bind(this));
		}.bind(this);
		
		$('findFriends').onkeydown = function(event) {
			var friends = this.friendList;
			var res = '';
			var s = $('findFriends').value;
			for(var i = 0; i<friends.length; i++) {
				if(friends[i].first_name.indexOf(s) > 0 || friends[i].last_name.indexOf(s) > 0 )
					res += friends[i].first_name + ' ' + friends[i].last_name + ' <br />';
			}
			$('gadgetContent').innerHTML = res;
		}.bind(this);
		
    	// create api
        this.api = new Facebook(this.api_key, this.secret_key, this.readyToProceed.bind(this));
    },
    
    readyToProceed: function() {
    	$('proceed').disabled = false;
    },
    readySession: function() {
    	$('showFriends').disabled = false;
    }
	
});

window.onload = function() { app = new Application(); };